#include "GameTrace.h"
#include <libloaderapi.h>

int CGameTrace::GetHitboxId()
{
	if (pHitboxData)
		return pHitboxData->m_nHitboxId;
	return 0;
}

int CGameTrace::GetHitgroup()
{
	if (pHitboxData)
		return pHitboxData->m_nHitGroup;
	return 0;
}

void Ray::InitFilter(CTraceFilter* pFilter, void* pEntity, uint64_t nMask, uint8_t nLayer1, uint16_t nLayer2)
{
	const auto client = reinterpret_cast<uintptr_t>(GetModuleHandle(L"client.dll"));
	using Filter = void (*)(CTraceFilter*, void*, uint64_t, uint8_t, uint16_t);
	Filter pfnCreateFilter = (Filter)(client + 0x1FB980);  //  sub_1801FA1A0((unsigned __int64)&v192);
	pfnCreateFilter(pFilter, pEntity, nMask, nLayer1, nLayer2);
}

CGameTrace Ray::TraceShape(Vector3& vecStart, Vector3& vecEnd, uintptr_t pSkipEntity, const uint64_t nMask)
{
    const auto client = reinterpret_cast<uintptr_t>(GetModuleHandle(L"client.dll"));
    using TTaceRay = bool (*)(void*, CTraceRay*, float*, float*, CTraceFilter*, CGameTrace*);
    using Filter = void (*)(CTraceFilter*, void*, uint64_t, uint8_t, uint16_t);

    float Start[3] = { vecStart.x,vecStart.y,vecStart.z };
    float End[3] = { vecEnd.x,vecEnd.y,vecEnd.z };

    TTaceRay pfnTraceShape = (TTaceRay)(client + 0x6EA130); //   return sub_1806E4D20(a1, (__int64)&v6, a2, a3, a4, a5);
    Filter pfnCreateFilter = (Filter)(client + 0x1FB980);  //  sub_1801FA1A0((unsigned __int64)&v192);
    void* pTraceManager = *(void**)(client + 0x1BCAB58);  //  sub_180579520((__int64)off_181BB9958, (unsigned __int64 *)v127, (__int64)&v116, &v148, (__int64)&v155);

    CTraceRay pTraceRay{};
    CTraceFilter pFilter{};
    CGameTrace pGameTrace{};
    pfnCreateFilter(&pFilter, (void*)pSkipEntity, nMask, 4, 15);
    pfnTraceShape(pTraceManager, &pTraceRay, Start, End, &pFilter, &pGameTrace);
    return pGameTrace;
}
